package collection2;

import java.util.Comparator;
import java.util.TreeSet;

/**
 * 实现字符串排序
 * 按长度排序，长度相同按字符串排序
 */
public class Demo04 {
    public static void main(String[] args) {
        //创建集合，并指定比较规则
        TreeSet<String> treeSet =  new TreeSet<String>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                int n1 = o1.length() - o2.length();
                int n2 =  o1.compareTo(o2);
                return n1==0? n2:n1;
            }
        });
        treeSet.add("zhangsan");
        treeSet.add("lisi");
        treeSet.add("zhaoliyin");
        treeSet.add("liuyifei");
        treeSet.add("lyangmi");
        for (String s : treeSet) {
            System.out.println(s + ":" + s.length());
        }


    }
}
